package org.achartengine.util;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import android.graphics.RectF;

public class RectFData
{
	/**
	 * item with xCoords Range
	 */
	private List<Map<Float[], String>> mXList;
	
	/**
	 * item with yCoords Range
	 */
	private List<Map<String, Float[]>> mYList;
	
	/**
	 * pie chart data
	 */
	private List<Map<String, Object>> mDataList;
	
	/**
	 * the item's title;
	 */
	private String itemTitle;
	
	public RectFData()
	{
	}
	
	/**
	 * init BarXYList
	 */
	private void getBarXYList()
	{
		mXList = RecfXEntryList.getList();
		mYList = RecfYEntryList.getList();
	}
	
	/**
	 * init PieList
	 */
	private void getPieDataList()
	{
		mDataList = PieCharEntryList.getList();
	}
	
	/**
	 * for bar chart
	 * 
	 * @param mRecf drawChart RectF
	 * @param item item description
	 */
	public void add2List(RectF mRecf, String item)
	{
		this.getBarXYList();
		ChartStyleCons.chartStyle = ChartStyleCons.BARCHART;
		Map<Float[], String> mXMap = new HashMap<Float[], String>();
		Map<String, Float[]> mYMap = new HashMap<String, Float[]>();
		float leftX = mRecf.left;
		float rightX = mRecf.right;
		float topY = mRecf.top;
		float buttomY = mRecf.bottom;
		mXMap.put(new Float[] { leftX, rightX }, item);
		mXList.add(mXMap);
		mYMap.put(item, new Float[] { topY, buttomY });
		mYList.add(mYMap);
	}
	
	/**
	 * for pie chart
	 * 
	 * @param radius circle radius
	 * @param currentAngle the angle for items
	 * @param angle startAngle
	 * @param mCurrentX center of xCoords
	 * @param mCurrentY center of yCoords
	 * @param item title of the item
	 */
	public void add2List(float radius, float currentAngle, float angle, int currentX, int currentY,
			String item)
	{
		this.getPieDataList();
		ChartStyleCons.chartStyle = ChartStyleCons.PIECHART;
		Map<String, Object> mPieData = new HashMap<String, Object>();
		mPieData.put("radius", radius);
		mPieData.put("currentAngle", currentAngle);
		mPieData.put("angle", angle);
		mPieData.put("currentX", currentX);
		mPieData.put("currentY", currentY);
		mPieData.put("item", item);
		mDataList.add(mPieData);
	}
	
	/**
	 * for bar chart click position is on item
	 * 
	 * @param x
	 * @param y
	 * @return
	 */
	public boolean isOnItem(float x, float y)
	{
		boolean temp = false;
		switch (ChartStyleCons.chartStyle)
		{
			case ChartStyleCons.BARCHART:
				this.getBarXYList();
				for (int i = 0; i < mXList.size(); i++)
				{
					Set<Float[]> mSet = mXList.get(i).keySet();
					Iterator<Float[]> mIter = mSet.iterator();
					Float[] mXFloat = mIter.next();
					/* 在x轴的范围内 */
					if (x >= mXFloat[0] && x <= mXFloat[1])
					{
						String mItemStr = mXList.get(i).get(mXFloat);
						Float[] mYFloat = mYList.get(i).get(mItemStr);
						if (y >= mYFloat[0] && y <= mYFloat[1])
						{
							itemTitle = mItemStr;
							temp = true;
						}
						break;
					}
				}
				break;
			case ChartStyleCons.PIECHART:
				this.getPieDataList();
				float radius = (Float) mDataList.get(0).get("radius");
				int currentX = (Integer) mDataList.get(0).get("currentX");
				int currentY = (Integer) mDataList.get(0).get("currentY");
				float pointR = (float) Math.sqrt((x - currentX) * (x - currentX) + (y - currentY)
						* (y - currentY));
				if (radius > pointR)
				{
					float mAngle = (float) (180 * Math.asin((x - currentX) / pointR) / Math.PI);
					if (y <= currentY)
					{
						mAngle = 275 + mAngle;
					}
					else if (y > currentY && mAngle < 0)
					{
						mAngle = 90 - mAngle;
					}
					for (int i = 0; i < mDataList.size(); i++)
					{
						float currentAngle = (Float) mDataList.get(i).get("currentAngle");
						float angle = (Float) mDataList.get(i).get("angle");
						String item = (String) mDataList.get(i).get("item");
						if (mAngle >= angle && mAngle <= currentAngle)
						{
							itemTitle = item;
							temp = true;
							System.out.println("itemTitle=" + itemTitle);
							break;
						}
					}
				}
				else
				{
					System.out.println("pointR=" + pointR + ",radius=" + radius);
				}
				break;
			default:
				break;
		}
		
		return temp;
	}
	
	/**
	 * @return "null" click point is not on the item;otherwise click point is on item
	 */
	public String getItemTitle()
	{
		if (itemTitle == null)
		{
			return "null";
		}
		else
		{
			return itemTitle;
		}
	}
	
	/**
	 * clear List
	 */
	public void clearList()
	{
		switch (ChartStyleCons.chartStyle)
		{
			case ChartStyleCons.BARCHART:
				RecfXEntryList.resetList();
				RecfYEntryList.resetList();
				break;
			case ChartStyleCons.PIECHART:
				PieCharEntryList.resetList();
				break;
		}
	}
}
